import { observable, action, toJS } from 'mobx';
import { WeaForm, WeaTableNew } from 'comsMobx';
import { Spin, Table, Button, Modal, Tooltip, Row, Col, message } from 'antd';
import { WeaLocaleProvider, WeaNewScroll, WeaLeftRightLayout, WeaInputNumber, WeaOrgTree, WeaCheckbox, WeaTools, WeaTop, WeaSearchGroup, WeaFormItem, WeaBrowser, WeaDateGroup, WeaInput, WeaSelect, WeaRightMenu, WeaDialog } from 'ecCom';
import * as request from '../../apis/plan/planReportDiagramApi'
import { getWeekOfYear } from '../../util/Util'
const getLabel = WeaLocaleProvider.getLabel;
export default class planReportDiagramStore {

    //loading
    @observable loading = false
    @observable orgLoading = false
    @observable exportLoading = false
    @observable titlename = ""
    @observable language = 7
    //当前选择tab key
    @observable selectTabKey = "2"
    //月还是周
    @observable dateType = "1"
    //日期选择数据
    @observable yearSelectData = []
    //日期值
    @observable dateValue = {
        year: this.getLastMonthDate().year,
        month: this.getLastMonthDate().month,
        week: this.getLastMonthDate().week,
        week2: this.getLastMonthDate().week
    }
    //tab是否显示
    @observable tabShowSearch = false

    //更多选项
    @observable moreSearchValues = {
        subType: "3",
        depType: "3"
    }

    //组织表格数据
    @observable orgTableDatas = []
    //员工表格数据
    @observable personTableDatas = []

    //员工页面分页数据
    @observable personPage = {
        pageSize: 10,
        pageNum: 1,
        total: 10,
    }
    @observable locationkey = new Date().getTime()
    @observable monthWeekSelectComDisplayKey = 'none'

    @observable selectedRowKeys = []; //选中的行
    @observable selectedRowValues = []; //选中的行值
    @observable showCharts = false; //是否显示图表
    chartOption = {}; //图表配置

    getLastMonthDate() {
        let now = new Date()
        const isJanuary = now.getMonth() == 0
        let year =(isJanuary && this.dateType == '1') ? now.getFullYear() - 1 : now.getFullYear()
        let month  =   isJanuary ? 12 : now.getMonth()
        let week = getWeekOfYear()
        if(year == new Date().getFullYear()-1 &&week == 1 ){
            week = 52
        }
        return {
            year,
            month,week
        }
    }

    @action('选中行改变')
    onSelectChange = (selectedRowKeys, selectedRowValues) => {
        this.selectedRowKeys = selectedRowKeys;
        this.selectedRowValues = selectedRowValues;
    }

    @action('查看图表')
    showChart = () => {
        //若选中行只有一行，则显示饼图，否则显示柱状图
        if (this.selectedRowValues.length > 1) {
            this.chartOption = {
                toolbox: {
                    feature: {
                        magicType: { show: true, type: ['line', 'bar'] },
                    },
                    y: 10,
                    x: '90%'
                },
                tooltip: {
                    trigger: 'axis',
                    axisPointer: {
                        type: 'cross',
                        crossStyle: {
                            color: '#999'
                        }
                    }
                },
                legend: {
                    data: this.selectedRowValues.map((item) => item.showname)
                },
                xAxis: {
                    data: [getLabel('508933', '需提交'), getLabel('32555', '未提交'), getLabel('509467', '处理草稿'), getLabel('509468', '处理审批中'), getLabel('509469', '处理退回'), getLabel('23774', '已完成'), getLabel('509470', '过期草稿'), getLabel('508852', '过期审批中'), getLabel('508853', '过期退回')],
                    axisPointer: {
                        type: 'shadow'
                    }
                },
                yAxis: {},
                series: this.getSeries()
            }
        } else if (this.selectedRowValues.length == 1) {
            let data = this.selectedRowValues[0];
            this.chartOption = {
                tooltip: {
                    trigger: 'item',
                    formatter: "{a} <br/>{b}: {c} ({d}%)"
                },
                title: {
                    text: data.showname + " " + getLabel('509473', '报告统计'),
                    x: 'center'
                },
                legend: {
                    orient: 'vertical',
                    x: 'left',
                    data: [getLabel('508933', '需提交'), getLabel('32555', '未提交'), getLabel('509467', '处理草稿'), getLabel('509468', '处理审批中'), getLabel('509469', '处理退回'), getLabel('23774', '已完成'), getLabel('509470', '过期草稿'), getLabel('508852', '过期审批中'), getLabel('508853', '过期退回')],
                },
                series: [
                    {
                        name: data.showname,
                        type: 'pie',
                        selectedMode: 'single',
                        radius: '75%',
                        center: ['50%', '50%'],
                        label: {
                            normal: {
                                position: 'inner'
                            }
                        },
                        labelLine: {
                            normal: {
                                show: false
                            }
                        },
                        data: [
                            { value: data.exist, name: getLabel('508933', '需提交') },
                            { value: data.without, name: getLabel('32555', '未提交') },
                            { value: data.scoring, name: getLabel('509467', '处理草稿') },
                            { value: data.assessing, name: getLabel('509468', '处理审批中') },
                            { value: data.back, name: getLabel('509469', '处理退回') },
                            { value: data.finish, name: getLabel('23774', '已完成') },
                            { value: data.oscoring, name: getLabel('509470', '过期草稿') },
                            { value: data.oassessing, name: getLabel('508852', '过期审批中') },
                            { value: data.oback, name: getLabel('508853', '过期退回') },
                        ]
                    }
                ]
            }
        }
        this.changeShowCharts();
    }

    //获取图表内容
    getSeries() {
        let series = [];
        this.selectedRowValues.map(item => {
            series.push(
                {
                    name: item.showname,
                    type: 'bar',
                    data: [item.exist, item.without, item.scoring, item.assessing, item.back, item.finish, item.oscoring, item.oassessing, item.oback]
                }
            )
        })
        return series;
    }

    @action('图表状态改变')
    changeShowCharts = () => {
        this.showCharts = !this.showCharts;
    }
    //日期变动事件
    @action onDateChange = (param, v) => {
        this.dateValue[param] = v;
    };

    //导出
    @action
    orgExport = () => {
        this.exportLoading = true
        request.exportExcel(this.getOrgRequestParams()).then(action((data) => {
            this.exportLoading = false
            if (data.url) {
                window.location.href = `${location.origin}${data.url}`
            }
        }))
    }

    @action
    personExport = () => {
        this.exportLoading = true
        request.exportExcel(this.getPersonRequestParams()).then(action((data) => {
            this.exportLoading = false
            if (data.url) {
                window.location.href = `${location.origin}${data.url}`
            }
        }))
    }


    //高级搜索变更事件
    @action
    onMoreSearchChange = (param, e) => {
        this.moreSearchValues[param] = e
    }

    //组织视图
    @action
    planReportDiagram = () => {
        this.loading = true
        this.orgLoading = true
        let t = this
        this.orgTableDatas = []
        request.planReportDiagram(this.getOrgRequestParams()).then(action(res => {

            if (res.api_status) {
                t.orgTableDatas = res.datas
                t.titlename = res.titlename
            } else {
                message.error(res.msg || getLabel('383746', '请求失败'));
            }
            this.loading = false
            this.orgLoading = false

        }));
    }

    //获取人员报表请求参数
    getPersonRequestParams = () => {
        return {
            year: this.dateValue.year,
            type: this.dateType == '1' ? 'month' : 'week',
            pagenum: this.personPage.pageNum,
            pagesize: this.personPage.pageSize,
            hrmids: this.moreSearchValues.hrmIds,
            subcompanyids: this.moreSearchValues.sub,
            departmentids: this.moreSearchValues.dep,
            cpyincludesub: this.moreSearchValues.subType,
            deptincludesub: this.moreSearchValues.depType,
            exportType: 'person'
        }
    }

    //获取组织报表请求参数
    getOrgRequestParams = () => {
        return {
            year: this.dateValue.year,
            month: this.dateValue.month,
            week: this.dateValue.week,
            week2: this.dateValue.week2,
            type: this.dateType == '1' ? 'month' : 'week',
            exportType: 'org'

        }
    }



    //获取人员报表数据
    @action
    planWorkerReportDiagram = () => {
        this.loading = true
        this.personTableDatas = []
        let t = this
        request.planWorkerReportDiagram(t.getPersonRequestParams()).then(action(res => {

            if (res.api_status) {
                t.personTableDatas = res.datas
                this.personPage.total = res.totalRecord
                this.titlename = res.titlename
                this.language = res.language
            } else {
                message.error(res.msg || getLabel('383746', '请求失败'));
            }
            this.loading = false
        }));
    }


    //获取年份
    @action
    getYearList = () => {
        let currentYear = parseInt(new Date().getFullYear());
        let yearList = []
        for (var i = currentYear; i > currentYear - 6; i--) {
            yearList.push({ showname: i, key: i, selected: i == currentYear })
        }
        this.yearSelectData = yearList
    }

    //获取当前周
    getWeekOfYear = () => {
        var today = new Date();
        var firstDay = new Date(today.getFullYear(), 0, 1);
        var dayOfWeek = firstDay.getDay();
        var spendDay = 1;
        if (dayOfWeek != 0) {
            spendDay = 7 - dayOfWeek + 1;
        }
        firstDay = new Date(today.getFullYear(), 0, 1 + spendDay);
        var d = Math.ceil((today.valueOf() - firstDay.valueOf()) / 86400000);
        var result = Math.ceil(d / 7);
        return result;
    };


}
